using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Collections.Specialized;


namespace WindowsGame1.CoreComponents
{
    public class MenuComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch = null;
        SpriteFont spriteFont;
        Color normalColor = Color.Yellow; //normal text color
        Color hiliteColor = Color.Red; //text color when selected
        KeyboardState oldState;
        Vector2 position = new Vector2(); //position of the menu on the screen
        int selectedIndex = 0;
        private StringCollection menuItems = new StringCollection();
        int width, height;


        public MenuComponent(Game1 game, SpriteFont spriteFont)
            : base(game)
        {
            this.spriteFont = spriteFont;
            spriteBatch =
            (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
        }
        public int Width
        {
            get { return width; }
        }

        public int Height
        {
            get { return height; }
        }

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set
            {
                //the next line makes sure that the value is not negative
                selectedIndex = (int)MathHelper.Clamp(
                value,
                0,
                menuItems.Count - 1);
            }
        }

        public Color NormalColor
        {
            get { return normalColor; }
            set { normalColor = value; }
        }

        public Color HiliteColor
        {
            get { return hiliteColor; }
            set { hiliteColor = value; }
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public void SetMenuItems(string[] items)
        {
            menuItems.Clear();
            menuItems.AddRange(items);
            CalculateBounds();
        }

        /// <summary>
        /// This calculates the height and the width of the menu items
        /// The width is set according to the longest string in the menu items
        /// and the height is increased depending on the number of items in the menu
        /// </summary>
        private void CalculateBounds()
        {
            width = 0;
            height = 0;
            foreach (string item in menuItems)
            {
                Vector2 size = spriteFont.MeasureString(item);
                if (size.X > width)
                    width = (int)size.X;
                height += spriteFont.LineSpacing;
            }
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        /// <summary>
        /// Controls the switching between menu entries using the arrow keys
        /// Depending on which key is pressed (up or down) the index of the 
        /// selected menu item either increments or decrements.
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            KeyboardState newState = Keyboard.GetState();
            if (CheckKey(Keys.Down))
            {
                selectedIndex++;
                if (selectedIndex == menuItems.Count)
                    selectedIndex = 0;
            }
            if (CheckKey(Keys.Up))
            {
                selectedIndex--;
                if (selectedIndex == -1)
                {
                    selectedIndex = menuItems.Count - 1;
                }
            }
            oldState = newState;
            base.Update(gameTime);
        }

        /// <summary>
        /// Determines if the key was pressed and released
        /// </summary>
        /// <param name="theKey"></param>
        /// <returns>True if the key was pressed and released, and false otherwise</returns>
        public bool CheckKey(Keys theKey)
        {
            KeyboardState newState = Keyboard.GetState();
            return oldState.IsKeyDown(theKey) && newState.IsKeyUp(theKey);
        }

        /// <summary>
        /// Draws the menu items
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            Vector2 menuPosition = Position;
            Color myColor;
            for (int i = 0; i < menuItems.Count; i++)
            {
                if (i == SelectedIndex)
                    myColor = HiliteColor;
                else
                    myColor = NormalColor;
                //Draws the shadow behind the text
                spriteBatch.DrawString(spriteFont,menuItems[i], menuPosition + Vector2.One,Color.Black);
                //and then the text itself on top
                spriteBatch.DrawString(spriteFont, menuItems[i],menuPosition,myColor);
                menuPosition.Y += spriteFont.LineSpacing;
            }
            base.Draw(gameTime);
        }
    }
}